package LawsonTransformJava.xref;

import java.io.IOException;
import java.sql.SQLException;
import java.util.TreeMap;

public class XrefPosition extends CrossRef{

	public class DftPos {
		private String pos = "";
		private String processLevel = "";
		private String department = "";
		private int expAcctUnit = 0;
		private int expAccount = 0;
		private String jobCode = "";
		private String locateCode = "";
		private int shift = 0;
		private String unionCode = "";
		private String bargainUnit = "";
		private int annualHours = 0;
		private String salaryClass = "";
		private int payFreq = 0;
		private String exemptEmp = "";
		private String otPlanCode = "";
		
		private final int dftAccountUnit = 99000;
		private final int dftAccount = 39999;

		/**
		 * @param pos - New Position
		 * @param processLevel - New Process Level
		 * @param department - New Department
		 * @param expAcctUnit - New Accounting Unit
		 * @param expAccount - New Account
		 * @param jobCode - New Job Code
		 * @param locatCode - New Location Code
		 * @param shift - New Shift
		 * @param unionCode - New Union Code
		 */
		
		public DftPos() {
			this.pos = "";
		}
		
		public DftPos(String pos, String processLevel, String department,
				int expAcctUnit, int expAccount, String jobCode,
				String locateCode, int shift, String unionCode,
				String bargainUnit, int annualHours, String salaryClass,
				int payFreq, String exemptEmp, String otPlanCode) {
			super();
			this.pos = pos;
			this.processLevel = processLevel;
			this.department = department;
			this.expAcctUnit = (expAcctUnit==0?dftAccountUnit:expAcctUnit);
			this.expAccount = (expAccount==0?dftAccount:expAccount);
			this.jobCode = jobCode;
			this.locateCode = locateCode;
			this.shift = shift;
			this.unionCode = unionCode;
			this.bargainUnit = bargainUnit;
			this.annualHours = annualHours;
			this.salaryClass = salaryClass;
			this.payFreq = payFreq;
			this.exemptEmp = exemptEmp;
			this.otPlanCode = otPlanCode;
		}
		
		public String getPos() {
			return pos;
		}
		public void setPos(String pos) {
			this.pos = pos;
		}
		public String getProcessLevel() {
			return processLevel;
		}
		public void setProcessLevel(String processLevel) {
			this.processLevel = processLevel;
		}
		public String getDepartment() {
			return department;
		}
		public void setDepartment(String department) {
			this.department = department;
		}
		public int getExpAcctUnit() {
			return expAcctUnit;
		}
		public void setExpAcctUnit(int expAcctUnit) {
			this.expAcctUnit = expAcctUnit;
		}
		public int getExpAccount() {
			return expAccount;
		}
		public void setExpAccount(int expAccount) {
			this.expAccount = expAccount;
		}
		public String getJobCode() {
			return jobCode;
		}
		public void setJobCode(String jobCode) {
			this.jobCode = jobCode;
		}
		public String getLocateCode() {
			return locateCode;
		}
		public void setLocateCode(String locateCode) {
			this.locateCode = locateCode;
		}
		public int getShift() {
			return shift;
		}
		public void setShift(int shift) {
			this.shift = shift;
		}
		public String getUnionCode() {
			return unionCode;
		}
		public void setUnionCode(String unionCode) {
			this.unionCode = unionCode;
		}
		public String getBargainUnit() {
			return bargainUnit;
		}
		public void setBargainUnit(String bargainUnit) {
			this.bargainUnit = bargainUnit;
		}
		public int getAnnualHours() {
			return annualHours;
		}
		public void setAnnualHours(int annualHours) {
			this.annualHours = annualHours;
		}
		public String getSalaryClass() {
			return salaryClass;
		}
		public void setSalaryClass(String salaryClass) {
			this.salaryClass = salaryClass;
		}
		public int getPayFreq() {
			return payFreq;
		}
		public void setPayFreq(int payFreq) {
			this.payFreq = payFreq;
		}
		public String getExemptEmp() {
			return exemptEmp;
		}
		public void setExemptEmp(String exemptEmp) {
			this.exemptEmp = exemptEmp;
		}
		public String getOtPlanCode() {
			return otPlanCode;
		}
		public void setOtPlanCode(String otPlanCode) {
			this.otPlanCode = otPlanCode;
		}
		public boolean isError() {
			return XrefPosition.isError();
		}
	}
	
	private final static String TABLE_NAME = "xPOSITION";
	static final String XLATE_ERROR = "POS_ERROR";
	public DftPos newPos;
	
//	private DAOFactoryAccessDB xref = new DAOFactoryAccessDB();
//	private Statement stmt = null;
//	private ResultSet rslt = null;
	
	TreeMap<String, DftPos> values = new TreeMap<String, DftPos>();  //Lookup mapping
	
	public XrefPosition() throws SQLException, IOException {
		stmt = xref.getDataStatement();
		rslt = stmt.executeQuery("SELECT * FROM " + TABLE_NAME);
		super.XLATE_ERROR=XLATE_ERROR;
		while (rslt.next()) {
			String old = rslt.getString(1);
			
			//Create newPos mapping from Cross Reference to old position.
			newPos = new DftPos(rslt.getString(2),		//position
					rslt.getString(4),					//process level
					rslt.getString(5),					//department
					rslt.getInt(6),						//exp account unit
					rslt.getInt(7),						//exp account
					rslt.getString(8),					//job code
					rslt.getString(9),					//locat code
					rslt.getInt(10),					//shift
					rslt.getString(11),					//union code
					rslt.getString(12),					//bargain unit
					rslt.getInt(13),					//annual hours
					rslt.getString(14),					//salary class
					rslt.getInt(15),					//pay frequency
					rslt.getString(16),					//exempt employee
					rslt.getString(17));				//ot plan code

			//Account for a blank BargainUnit which is valid but returns a null
			if (newPos.getBargainUnit()==null)
				newPos.setBargainUnit("");
			//Write the mapped values to the TreeMap
			this.values.put(old, newPos);
		}
		//Release the JDBC recourses
		super.close();
	}
	
	public DftPos findDftPosValues(String lookup) {
		DftPos dftpos = null;
		//Strip out Quotes
		String strip = lookup.replaceAll("\"", "");
		//If lookup value is blank coming in, then pass it back out without a lookup
//		if (strip.equals(""))
//			return null;

		//Lookup value
		dftpos = (DftPos)values.get(String.valueOf(strip));
		//If lookup not found or lookup is blank then return XLATE_ERROR
		if (dftpos==null) {
			dftpos = new DftPos();
			if ( strip.equals(""))
				strip = "BLANK";
			//Set Error Flag
			errFound = true;
			dftpos.setPos(XLATE_ERROR + "_" + strip);
		}
		return dftpos;
	}
	
	@Override
	public String xlate(String lookup) {
		//Strip out Quotes
		String strip = lookup.replaceAll("\"", "");
		//If lookup value is blank coming in, then pass it back out without a lookup
		if (strip.equals(""))
			return strip;

		//Value found
		DftPos newX = (DftPos)values.get(String.valueOf(strip));

		//Lookup value; if not found send back blanks if allowed otherwise send XLATE_ERROR
		if (allowBlanks)
			return (newX==null?"":"\"" + newX.getPos() + "\"");
		else {
			if (newX==null) {
				//Set Error Flag
				errFound = true;
				return XLATE_ERROR + "_" + strip;
			} else
				return "\"" + newX.getPos() + "\"";
		}
	}	
}
